//
//  PersonalCenterViewController.m
//  HZHome
//
//  Created by SunShine on 13-10-14.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "LoginViewController.h"
#import "AdviceChooseCell.h"
#import "AdviceButtonCell.h"
#import "AdviceTextCell.h"
#import "HelpListCell.h"
#import "PersonalInfoViewController.h"
#import "ModifyPasswordViewController.h"
#import "WebViewController.h"
#import "MessageListViewController.h"
#import "RegisterViewController.h"
#import "MessageListService.h"
#import "MyMessageItem.h"
#import "NoticesItem.h"
#import "MyConcernNoticesDetailViewController.h"
#import "SupervisionDetailController.h"
#import "ResidentDataItem.h"
#import "ResidentComplainRecordController.h"
#import "AdvisoryDetailInfoViewController.h"
#import "MessageService.h"
#import "Feedback.h"
#import "FeedbackViewController.h"
#import "NSObject+propertyList.h"
#import "PrintObject.h"

#define INPUT_VIEW_OFFSET 150.0

@interface PersonalCenterViewController ()<SegmentViewDelegate>
@property (strong, nonatomic) UIBarButtonItem *rightBt;
//@property (strong, nonatomic) UISegmentedControl *practicabilitySegment;
//@property (strong, nonatomic) UISegmentedControl *easeUsablitySegment;
@property (assign, nonatomic) float practicability;
@property (assign, nonatomic) float easeUsablity;
@property (strong, nonatomic) UITextView *adviceTextView;
@property (strong, nonatomic) MessageService *dataService;
@property (assign, nonatomic) BOOL loginFlag;
@property (strong, nonatomic) SegmentView *segmentView;
//@property (strong, nonatomic) NSArray *dataList;


-(void)rightBtAction:(id)sender;
@end

@implementation PersonalCenterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"我的海珠";
 
    self.dataService = [MessageService new];
    _dataService.delegate = self;
    _dataService.noticeDelegate = self; //绑定目标，用于回调方法
    //!= 1 代表是是否从底部导航跳进来的，如果是，隐藏后退按扭， 
    if(_isBottomNavigationGoIn != 1){
        self.navigationItem.leftBarButtonItem.customView.hidden = YES;
    }
//    [self initSegment];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //控制显示的是登录或者注销
    self.navigationItem.rightBarButtonItem = nil;
    UserInfoService *userService=[UserInfoService getUserInfoService];
    //UserInfo *userInfo=userService.userInfo;
    NSString *currentUserName = userService.currentLoginUserName;
    NSLog(@"currentUserName = %@",currentUserName);
    if (currentUserName && currentUserName.length > 0&&isStart==1) {
        self.navigationItem.rightBarButtonItem = rightNavBtnWithImgName(@"注销", self, @selector(rightBtAction:));
        self.loginFlag = YES;
    } else {
        if (isStart==0) {
//            self.navigationItem.rightBarButtonItem = getCustomNavButton(@"注册", self, @selector(rightBtAction:));
            self.navigationItem.rightBarButtonItem = nil;
        }
        if (isStart==2) {
            self.navigationItem.rightBarButtonItem = getCustomNavButton(@"登录", self, @selector(rightBtAction:));
        }
        self.loginFlag = NO;
    }
    [self initSegment];

    //load message
    //[_dataService loadMessage];//去掉登录限制  //加载我的消息 lhm
    [self startLoadingViewWithText:@"正在载入..."];
    [_dataService loadMessageList:^(NSArray *data) {
        
        _dataService.dataList = data;
        [self stopLoadingView];
     [_tableView reloadData];
    } Failure:^(NSString *msg) {
        [self showAlertWithTitle:@"错误" Msg:msg];
        [self stopLoadingView];
    }];
    
    
    //请求服务器 PERSONAL_ALL_MESSAGE_LIST_URL  list
//    [[MessageListService new]queryMyMessageList:^(NSArray *data) {
// 
//        self.dataList = data;
//        } Failure:^(NSString *msg) {
//        
//    }];
    //[self startLoadingViewWithText:@"正在读取消息..."];
//    if ([UserInfoService getUserInfoService].currentLoginUserName) {
//        [_dataService loadMessage];//去掉登录限制
//        [self startLoadingViewWithText:@"正在读取消息..."];
//    }
//    self.navigationItem.leftBarButtonItem = nil;
    self.tableView.scrollEnabled = YES;
}

-(void)initSegment
{
    if (self.segmentView) {
        [self.segmentView removeFromSuperview];
        self.segmentView = nil;
    }
    //init segment
    NSArray *array = nil;
    if (_loginFlag) {
        array = @[@{@"image":@"我的消息",@"light":@"我的消息_选中"},
                  @{@"image":@"个人信息",@"light":@"个人信息_选中"},
                  @{@"image":@"意见反馈",@"light":@"意见反馈_选中"},
                  @{@"image":@"帮助",@"light":@"帮助_选中"}];
    } else {
        array = @[@{@"image":@"我的消息",@"light":@"我的消息_选中"},
                  @{@"image":@"意见反馈",@"light":@"意见反馈_选中"},
                  @{@"image":@"帮助",@"light":@"帮助_选中"}];
    }
    self.segmentView = [[SegmentView alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];
    [self.segmentView initView:array];
    self.segmentView.delegate = self;
    [self.view addSubview:self.segmentView];
    [self didSelectSegmentAtIndex:0];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
//设置右侧按扭
#pragma mark - Button Methods
-(void)rightBtAction:(id)sender
{
    if (isStart==0) {
        RegisterViewController *registerVC = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
        [self.navigationController pushViewController:registerVC animated:YES];
    }
    if (isStart==1) {
        //进行注销
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否注销" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
    if (isStart==2) {
        //进行登录
        LoginViewController *loginVC = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}
//提示框
#pragma mark - Alert Delegate Methods
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            //取消
            break;
        case 1:
            //注销
            [[UserInfoService getUserInfoService] logout];
            self.navigationItem.rightBarButtonItem = nil;
            self.navigationItem.rightBarButtonItem = getCustomNavButton(@"登录", self, @selector(rightBtAction:));
            self.loginFlag = NO;
            isStart=2;
            [self initSegment];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"interIndexViewController" object:nil];
            [self.tabBarController setSelectedIndex:3];
            
            break;
        default:
            break;
    }
}
//tab改变
#pragma mark - Segment Method
- (void)didSelectSegmentAtIndex:(int)index
{
    [self.tableView reloadData];
    NSInteger selected = index;
    if ((_loginFlag && selected == 2)||(!_loginFlag && selected == 0)) {
        //登录状态下的第3个或者非登录状态下的第1个为意见反馈
        self.tableView.scrollEnabled = YES;
    } else {
        self.tableView.scrollEnabled = NO;
    }
    if (selected == 0) {
        self.tableView.scrollEnabled = YES;

    }
}

- (IBAction)segmentValueChangedAction:(id)sender
{
    [self.tableView reloadData];
    NSInteger selected = self.segmentView.segmentIndex;
    if ((_loginFlag && selected == 2)||(!_loginFlag && selected == 0)) {
        //登录状态下的第3个或者非登录状态下的第1个为意见反馈
        self.tableView.scrollEnabled = YES;
    } else {
        self.tableView.scrollEnabled = NO;
    }
}
//tab改变
#pragma mark - Table Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger num = 1;
    if (_loginFlag) {
        //登录状态下，意见反馈是第3个
        if (self.segmentView.segmentIndex == 2) {
            num = 3;//意见反馈那个页面分成三个section
        }
    } else {
        //未登录状态下，意见反馈是第2个
        if (self.segmentView.segmentIndex == 1) {
            num = 3;//意见反馈那个页面分成三个section
        }
    }
    return num;
}
//设置item数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num = 0;
    NSInteger selected = self.segmentView.segmentIndex;
    if (selected == 0) {
        //第1个为我的消息
        self.messageItemNum = _dataService.dataList.count;
        return self.messageItemNum;
    } else if (_loginFlag && selected == 1) {
        //登录状态下，第2个为个人信息
        num = 2;//个人信息有两项
    } else if ((_loginFlag && selected == 2)||(!_loginFlag && selected == 1)) {
        //登录状态下的第3个或者非登录状态下的第2个为意见反馈
        if (section == 0) {
            num = 2;//意见反馈第一section有2个
        } else {
            num = 1;//意见反馈第二第三section各有1个
        }
    } else if ((_loginFlag && selected == 3)||(!_loginFlag && selected == 2)) {
        //登录状态下的第4个或者非登录状态下的第3个为帮助
        num = 4;//帮助页面有4个
    }
    return num;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger selected = self.segmentView.segmentIndex;
    if ((_loginFlag && selected == 2)||(!_loginFlag && selected == 1)) {
        //登录状态下的第3个或者非登录状态下的第2个为意见反馈
        if (indexPath.section == 1) {
            return 176.0f;//意见反馈的第二个section为4倍高
        }
    }
    return 44.0f;
}

//先要设Cell可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger selected = self.segmentView.segmentIndex;
    if (selected == 0) {//如果是我的消息才能删除
        NSDictionary *dic = [_dataService.dataList objectAtIndex:indexPath.row];
        NSString *type = [dic objectForKey:@"dataType"];
        if([type isEqualToString:@"tzgg"]||[type isEqualToString:@"yjfk"]){//如果是通知公告 或 意见反馈 才能显视删除
            return YES;
        }
    }
    return NO;
}
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
//取得url用了删除数据
-(NSString*)getDeleteUrl:(NSString*)dataType{
    if ( [dataType isEqualToString:@"tzgg"]) {
        return PERSONAL_MESSAGE_TZGG_DELETE_URL;
    }
    if ( [dataType isEqualToString:@"yjfk"] ) {
        return PERSONAL_MESSAGE_YJFK_DELETE_URL;
    }
    return @"";
}
//删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSDictionary *dic = [_dataService.dataList objectAtIndex:indexPath.row];
        NSString *type = [dic objectForKey:@"dataType"];
        NSString* dataId = [dic objectForKey:@"dataId"];
        //请求后台删除数据，如果成功，删除tableView数据
        [self startLoadingViewWithText:@"正在加载"];
        
        [[MessageService alloc]deleteMessageById:[[self getDeleteUrl:type]stringByAppendingString:dataId] Success:^(Boolean *flag) {
            if (flag) {
                int row = indexPath.row;
                self.dataList = [[NSMutableArray alloc]init];
                [self.dataList addObjectsFromArray:_dataService.dataList];
                
                [self.dataList removeObjectAtIndex:row];//删除数据
                _dataService.dataList = self.dataList;
                self.messageItemNum -=1;//行数减1
                
                //更新tableView数据
                [tableView beginUpdates];
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
                [tableView endUpdates];
            }
            
            [self stopLoadingView];
        } Failure:^(NSString *msg) {
            [self showAlertWithTitle:@"提示" Msg:@"删除失败"];
            [self stopLoadingView];
        }];
        
        
        
    }
}

//设置值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    NSInteger selected = self.segmentView.segmentIndex;
    if (selected == 0) {
        //第1个为我的消息
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"PersonalTableViewCell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:@"PersonalTableViewCell"]  ;
        }
        
        NSDictionary *dic = [_dataService.dataList objectAtIndex:indexPath.row];
        NSString *colosCode = [dic objectForKey:@"dataReadStatus"];
        if ([@"0" isEqualToString:colosCode]) {//等于0设为红色
            cell.textLabel.textColor = [UIColor redColor];
        }
        cell.textLabel.text = [dic objectForKey:@"dataTitle"];
        
        
        return cell;
    } else if (_loginFlag && selected == 1) {
        //登录状态下，第2个为个人信息
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonalTableViewCell"];
        if (indexPath.row == 0) {
            cell.textLabel.text = @"我的信息";
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"密码修改";
        } else {
            cell.textLabel.text = @"手机号验证";
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    } else if ((_loginFlag && selected == 2)||(!_loginFlag && selected == 1)) {
        //登录状态下的第3个或者非登录状态下的第2个为意见反馈
        if (indexPath.section == 0) {//第一section选择实用性和易用性
            cell = [[[NSBundle mainBundle]loadNibNamed:@"AdviceChooseCell" owner:self options:nil]objectAtIndex:0];
            if (indexPath.row == 0) {
                ((AdviceChooseCell *)cell).titleLabel.text = @"实用性";
                ((AdviceChooseCell *)cell).starRating.returnBlock = ^(float rate){
                    NSLog(@"实用性打分:%f", rate);
                    self.practicability = rate;
                };
            } else {
                ((AdviceChooseCell *)cell).titleLabel.text = @"易用性";
                ((AdviceChooseCell *)cell).starRating.returnBlock = ^(float rate){
                    NSLog(@"易用性打分:%f", rate);
                    self.easeUsablity = rate;
                };
            }
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        } else if (indexPath.section == 1) {//第二section输入意见
            cell = [[[NSBundle mainBundle]loadNibNamed:@"AdviceTextCell" owner:self options:nil]objectAtIndex:0];
            ((AdviceTextCell *)cell).adviceTextView.inputAccessoryView = [self getInputAccessoryView];
            self.adviceTextView = ((AdviceTextCell *)cell).adviceTextView;
            ((AdviceTextCell *)cell).adviceTextView.delegate = self;
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        } else {//第三section是按钮
            cell = [[[NSBundle mainBundle]loadNibNamed:@"AdviceButtonCell" owner:self options:nil]objectAtIndex:0];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    } else if ((_loginFlag && selected == 3)||(!_loginFlag && selected == 2)) {
        //登录状态下的第4个或者非登录状态下的第3个为帮助
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HelpListCell" owner:self options:nil]objectAtIndex:0];
        if (indexPath.row == 0) {
            ((HelpListCell *)cell).titleLabel.text = @"帮助";
            ((HelpListCell *)cell).flagImageView.hidden = YES;
            ((HelpListCell *)cell).indImageView.hidden = NO;
        }
        else if (indexPath.row == 1) {
            ((HelpListCell *)cell).titleLabel.text = @"关于";
            ((HelpListCell *)cell).flagImageView.hidden = YES;
            ((HelpListCell *)cell).indImageView.hidden = NO;
            
        } else if(indexPath.row == 2) {
            ((HelpListCell *)cell).titleLabel.text = @"分享";
            ((HelpListCell *)cell).flagImageView.hidden = YES;
            ((HelpListCell *)cell).indImageView.hidden = NO;
            
        }
//        else if(indexPath.row == 3) {
//            ((HelpListCell *)cell).titleLabel.text = @"更新";
//            ((HelpListCell *)cell).flagImageView.hidden = YES;
//            ((HelpListCell *)cell).indImageView.hidden = NO;
//            
//        }
        else if(indexPath.row == 3) {
            ((HelpListCell *)cell).titleLabel.text = @"通知提醒";
            ((HelpListCell *)cell).flagImageView.hidden = YES;
            ((HelpListCell *)cell).indImageView.hidden = YES;
            ((HelpListCell *)cell).switchCtr.hidden = NO;
        } else {
            ((HelpListCell *)cell).titleLabel.text = @"绑定手机";
            ((HelpListCell *)cell).flagImageView.hidden = YES;
            ((HelpListCell *)cell).indImageView.hidden = NO;
            
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        return cell;
    }
    return cell;
}
//事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSInteger selected = self.segmentView.segmentIndex;
    if (selected == 0) {
        //第1个为我的消息
        NSDictionary *dic = [_dataService.dataList objectAtIndex:indexPath.row];
         //[dic objectForKey:@"dataTitle"];
        // MyMessageItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
         [self messageEvent:dic];

        return;
    } else if (_loginFlag && selected == 1) {
        //登录状态下，第2个为个人信息
        if (indexPath.row == 0) {
            //跳转 我的信息
            PersonalInfoViewController *infoVC = [[PersonalInfoViewController alloc]initWithNibName:@"PersonalInfoViewController" bundle:nil];
            infoVC.hidesBottomBarWhenPushed = YES;

            [self.navigationController pushViewController:infoVC animated:YES];
        } else {
            //跳转 密码修改
            ModifyPasswordViewController *modifyVC = [[ModifyPasswordViewController alloc]initWithNibName:@"ModifyPasswordViewController" bundle:nil];
            modifyVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:modifyVC animated:YES];
        }
        return;
    } else if ((_loginFlag && selected == 2)||(!_loginFlag && selected == 1)) {
        //登录状态下的第3个或者非登录状态下的第2个为意见反馈
        if (indexPath.section == 2) {
            //提交按钮的操作
            [self makeFeedBack];
        }
        return;
       
    } else if ((_loginFlag && selected == 3)||(!_loginFlag && selected == 2)) {
        //登录状态下的第4个或者非登录状态下的第2个为帮助
        if (indexPath.row == 0) {
            //跳转 登录页面
//            [self login];
            [self showWebView:@"Help"];

        }
        if (indexPath.row == 1) {
            //跳转 帮助页面
//            [self showWebView:@"Help"];
            [self showWebView:@"About"];

        } else if(indexPath.row == 2) {
            //跳转 关于页面
//            [self showWebView:@"About"];
        } else if(indexPath.row == 3) {
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"]];
//            if([MFMessageComposeViewController canSendText]){
//                [self displaySMSComposeSheet];
//            }else{
//                NSLog(@"Device not configured to send SMS.");
//            }
        }
        else {
            //进行更新检查
        }
        return;
    }
}
//跳转到登录页
-(void) login{
    if (_loginFlag) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户已经登" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    LoginViewController *loginVC = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
    [self.navigationController pushViewController:loginVC animated:YES];
}
#pragma mark - MSM
-(void) displaySMSComposeSheet{
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate=self;
    
    picker.body = @"海珠家园 http://www.haizhu.gov.cn/app/";
    [self presentViewController:picker
                       animated:YES
                     completion:NULL];
}
#pragma mark - MSM2
-(void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MessageComposeResultCancelled:
            NSLog(@"Result: SMS sending canceled");
            break;
        case MessageComposeResultSent:
            NSLog(@"Result: SMS sent");
            break;
        case MessageComposeResultFailed:
            NSLog(@"Result: SMS sending failed");
            break;
        default:
            NSLog(@"Result: SMS not sent");
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark - MSM3
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}

#pragma mark - Text View Delegate
//开始输入的时候向上移动整个界面，这个输入框不会被挡住
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.3f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y - INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}

//输入结束的时候把整个界面向下移动，这个能恢复正常
-(void)textViewDidEndEditing:(UITextView *)textView
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.3f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y + INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}

#pragma mark - Feedback Method
-(void)makeFeedBack
{
    NSString *content = _adviceTextView.text;
    if (!content || content.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入反馈内容" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSLog(@"self.practicability = %f",self.practicability);
    NSLog(@"self.easeUsablity = %f",self.easeUsablity);

    [self startLoadingViewWithText:@"正在提交..."];
    [UserInfoService getUserInfoService].delegate = self;
    [[UserInfoService getUserInfoService] userFeedbackWithContent:content Practicality:self.practicability EaseUse:self.easeUsablity];
}

#pragma mark - Show WebView
-(void)showWebView:(NSString *)key
{
    NSString *url;
    NSString *title;
    if ([key isEqualToString:@"About"]) {
        url = @"about";
        title = @"关于";
    } else if ([key isEqualToString:@"Help"]) {
        url = @"help";
        title =@"帮助";
    }
    if (url && url.length > 0) {
        WebViewController *webVC = [[WebViewController alloc]initWithNibName:@"WebViewController" bundle:nil];
//        webVC.url = url;
        webVC.title = title;
        webVC.name = url;
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];
//        [webVC loadFileWithName:url];
    }
}

#pragma mark - User Service Delegate
-(void)loadUserInfoFinished:(id)result
{
    [self stopLoadingView];
    self.adviceTextView.text = @"";
//    self.practicabilitySegment.selectedSegmentIndex = 0;
//    self.easeUsablitySegment.selectedSegmentIndex = 0;
}

-(void)loadUserInfoFaild:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [self stopLoadingView];
    
}

#pragma mark - Data Service
-(void)loadDataFinished
{
    [self stopLoadingView];
    [_tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [self stopLoadingView];
}

//我的消息 事件跳转
-(void)messageEvent:(NSDictionary *)item{
    NSString *type =[item objectForKey:@"dataType"]  ;
    if([type isEqualToString:@"tzgg"]){//如果是通知公告
        [self startLoadingViewWithText:@"正在载入..."];
       // NSLog(@"阅读状态 = %@ ",[item objectForKey:@"dataReadStatus"] );
        NSString *dataReadStatus = [item objectForKey:@"dataReadStatus"];
        if ( [dataReadStatus isEqualToString:@"0"]) {
            //标记已读
            [_dataService markMessageReadForType:@"tzgg" AndID:[item objectForKey:@"dataId"]];
        }
        //取详情并跳转
        NSString* ids = [item objectForKey:@"obj"] ;
        [_dataService loadNoticeForID: ids ];
        
    }else if([type isEqualToString:@"wyjd"]){//我要监督 用id取详情，设已读，跳转 url后加id
        [self startLoadingViewWithText:@"正在载入..."];
        [_dataService loadMessageWYJD:[item objectForKey:@"dataId"]];
         //标记已读
        if ([[item objectForKey:@"dataReadStatus"] isEqualToString:@"0"]) {
            //标记已读
            [_dataService markMessageReadForType:@"wyjd" AndID:[item objectForKey:@"dataId"]];
        }
        
    }else if([type isEqualToString:@"qzxx"]){//如果是区长信箱qzxx
        [self startLoadingViewWithText:@"正在载入..."];
        
        if ([[item objectForKey:@"dataReadStatus"] isEqualToString:@"0"]) {
            //标记已读
            [_dataService markMessageReadForType:@"qzxx" AndID:[item objectForKey:@"dataId"]];
        }
        NSDictionary *detailItem = [item objectForKey:@"obj"];
        [_dataService queryQzxxInfo:[detailItem objectForKey:@"slid"] withName:[detailItem objectForKey:@"xfuserName"] Success:^(NSDictionary *json) {
            NSDictionary *receiveXFinfo = [json objectForKey:@"receiveXFinfo"];
            ResidentDataItem *tempItem=[[ResidentDataItem alloc] init];
            NSArray *properties = [tempItem getPropertyList];
            for (NSString *propertyName in properties) {
                NSString *key = propertyName;
                if ([propertyName isEqualToString:@"Id"]) {
                    key = @"id";//因为属性不能叫做id所以把I变成了大写
                }
                [tempItem setValue:[receiveXFinfo objectForKey:key] forKey:propertyName];
            }
            NSArray *huifu = [json objectForKey: @"receiveTraninfoList"];
            NSString *receive=@"";
            NSString *t=@"\n\t..............................................................\n\n";
            for (int i = 0; i<[huifu count]; i++) {
                NSDictionary *content = [huifu objectAtIndex:i];
                //receive += [content objectForKey:@"disposerOpinion"];
                receive = [receive stringByAppendingFormat:@"%@,%@",[content objectForKey:@"disposerOpinion"], t];
                
            }
            [self stopLoadingView];
            
            //取详情并跳转
            ResidentComplainRecordController *vc = [[ResidentComplainRecordController alloc]initWithNibName:@"ResidentComplainRecordController" bundle:nil];
            vc.item =  tempItem;
            vc.item.receive = receive;
            [self.navigationController pushViewController:vc animated:YES];
        } Failure:^(NSString *msg) {
            [self stopLoadingView];
        }];
        
        //对象转换
 
         
        
        
    }else if([type isEqualToString:@"zczx"]){//如果是咨询
        if ([[item objectForKey:@"dataReadStatus"] isEqualToString:@"0"]) {
            //标记已读
            [_dataService markMessageReadForType:@"zczx" AndID:[item objectForKey:@"dataId"]];
            
        }
        
        AdvisoryDetailInfoViewController *vc = [[AdvisoryDetailInfoViewController alloc]initWithNibName:@"AdvisoryDetailInfoViewController" bundle:nil];
        vc.config = [NSMutableDictionary dictionaryWithObjectsAndKeys:[item objectForKey:@"dataId"], @"id",  nil];
        [self.navigationController pushViewController:vc animated:YES];
    }else if([type isEqualToString:@"yjfk"]){//意见反馈
        if ([[item objectForKey:@"dataReadStatus"] isEqualToString:@"0"]) {
            //标记已读
            [_dataService markMessageReadForType:@"yjfk" AndID:[item objectForKey:@"dataId"]];
        }

        //读取票面信息
        [self startLoadingViewWithText:@"正在加载"];
        [[MessageService new] getFeelback:[item objectForKey:@"dataId"]  Success:^(NSDictionary *data) {
            Feedback *detail = getObjectFromDic([Feedback class], data);
            [self stopLoadingView];
            
            FeedbackViewController *fvc = [[FeedbackViewController alloc]initWithNibName:@"FeedbackViewController" bundle:nil];
            fvc.feedack = detail;
            [self.navigationController pushViewController:fvc animated:YES];
        } Failure:^(NSString *msg) {
            [self stopLoadingView];
        }];
       
    }
}


#pragma mark - Notice Delegate
-(void)loadNoticeFail:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
//通知公告详情回调方法
#pragma mark - Notice Delegate
-(void)loadNoticeSuccess:(id)data
{
    [self stopLoadingView];
    TZGGDetailItem *detailItem = (TZGGDetailItem *)data;
    NoticesItem *item = [NoticesItem new];
    item.title = detailItem.title;
    item.detail = detailItem.detail;
    item.fileUrl = detailItem.fileUrl;
    MyConcernNoticesDetailViewController *detailVC = [[MyConcernNoticesDetailViewController alloc]initWithNibName:@"MyConcernNoticesDetailViewController" bundle:nil];
    detailVC.config = [NSDictionary dictionaryWithObjectsAndKeys:item, @"dataItem", nil];
    [self.navigationController pushViewController:detailVC animated:YES];
}
//我要监督回调
#pragma mark
-(void)loadWYJDSuccess:(id)data{
    NSLog(@"监督回调--->>");
    [self stopLoadingView];
    SupervisionDetailController *vc = [[SupervisionDetailController alloc]initWithNibName:@"SupervisionDetailController" bundle:nil];
    ComplainEntity *dataItem =(ComplainEntity*)data;

    vc.config = [NSMutableDictionary dictionaryWithObjectsAndKeys:dataItem, @"dataItem", @"我要监督", @"key", nil];
    [self.navigationController pushViewController:vc animated:YES];
}
//提示框
-(void)showAlertWithTitle:(NSString *)title Msg:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

@end
